package mollmapsgenerator;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Node;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.SAXReader;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hanis
 */
public class LandPagesGenerator {

    public LandPagesGenerator() {

    }

    public void generateLand(String land) {
        try {
            System.out.println("..creating " + land);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = factory.newTransformer(new StreamSource(new File("landStyle.xsl")));
            Document document = getDocument(new File(land + ".xml"));
            transformer.transform(new DocumentSource(document), new StreamResult(new FileWriter("/home/hanis/ttmp/mollova-sbirka/" + land + "/index.html")));
        } catch (IOException ex) {
            Logger.getLogger(LandPagesGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerConfigurationException ex) {
            Logger.getLogger(LandPagesGenerator.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            Logger.getLogger(LandPagesGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


    public void generateAllLands() {
        Document document = getDocument(new File("land_list.xml"));
        List<Node> lands = document.selectNodes("list/land");
        for (Node node : lands) {
            String land = node.getText();
            generateLand(land);
        }

    }


    public Document getDocument(File file) {
        Document document = null;
        try {
            DocumentFactory factory = new DocumentFactory();
            SAXReader reader = new SAXReader();
            document = reader.read(file);
        } catch (DocumentException ex) {
            Logger.getLogger(LandPagesGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return document;
    }


    public static void main(String[] args) {
        LandPagesGenerator gen = new LandPagesGenerator();

        gen.generateAllLands();
    }
}
